package advertising

import (
	"context"
	"device-admin/internal/app/grpc/client"
	"device-admin/internal/app/grpc/protoc/advertisingc"

	"192.168.1.75/go-pkg/errorx"
)

type Advertising struct{}

// 广告管理 -- 节目清单
func (Advertising) AdvertisingList(ctx context.Context, robotName, terminalType string, page, limit int32) (data []*advertisingc.AdvertisingMaterialListRsp_MaterialList, count int64, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	var TerminalTypeMap = map[string]int32{
		"97": 1,
		"98": 2,
	}
	var params = &advertisingc.AdvertisingMaterialListReq{}
	params.RobotName = robotName
	params.TerminalType = TerminalTypeMap[terminalType]
	params.Page = page
	params.Limit = limit
	params.RobotName = robotName
	rsp, rspErr := cli.AdvertisingMaterialList(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		if rsp != nil {
			return data, count, errorx.New(rsp.Msg, int(rsp.Code))
		}
		return
	}

	if rsp.List == nil {
		rsp.List = []*advertisingc.AdvertisingMaterialListRsp_MaterialList{}
	}

	return rsp.List, rsp.Total, nil
}

// 广告管理 -- 广告播放清单
func (Advertising) AdvertisingPlayList(ctx context.Context, robotName, terminalType string, page, limit int32) (data []*advertisingc.AdvertisingPlayListRsp_MaterialList, count int64, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	var TerminalTypeMap = map[string]int32{
		"97": 1,
		"98": 2,
	}
	var params = &advertisingc.AdvertisingPlayListReq{}
	params.RobotName = robotName
	params.TerminalType = TerminalTypeMap[terminalType]
	params.Page = page
	params.Limit = limit
	rsp, rspErr := cli.AdvertisingPlayList(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		if rsp != nil {
			return data, count, errorx.New(rsp.Msg, int(rsp.Code))
		}
		return
	}

	if rsp.List == nil {
		rsp.List = []*advertisingc.AdvertisingPlayListRsp_MaterialList{}
	}

	return rsp.List, rsp.Total, nil
}
